<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="车辆品牌">
        <el-input v-model="formInline.vehicleBrand" placeholder="请输入车辆品牌"></el-input>
      </el-form-item>
      <el-form-item label="日租金">
        <el-input v-model="formInline.startdayPrice" placeholder="请输入"></el-input>
      </el-form-item>__
      <el-form-item>
        <el-input v-model="formInline.enddayPrice" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="list">查询</el-button>
        <el-button type="primary" @click="openAdd">添加</el-button>
        <el-button type="primary" @click="getExcel">导出</el-button>
      </el-form-item>
      <el-form-item>
        <el-upload
            class="upload-demo"
            drag
            action="http://localhost:10020/vehicle/import"
            multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传xls/xlsx文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="vehicleId"
        label="车辆唯一编号"
        width="120">
      </el-table-column>
      <el-table-column
        prop="plateNumber"
        label="车牌号"
        width="120">
      </el-table-column>
      <el-table-column
        prop="vehicleType"
        label="车型"
        width="120">
      </el-table-column>
      <el-table-column
        prop="color"
        label="颜色"
        width="120">
      </el-table-column>
      <el-table-column
        prop="vehicleBrand"
        label="车辆品牌"
        width="120">
        <template slot-scope="s">
          <span v-html="s.row.vehicleBrand"></span>
        </template>
      </el-table-column>
      <el-table-column
        prop="vehicleEdition"
        label="车联系统版本"
        width="120">
      </el-table-column>
      <el-table-column
        prop="makeYear"
        label="制造年份"
        width="120">
      </el-table-column>
      <el-table-column
          prop="dayPrice"
          label="日租金"
          width="120">
<!--        <template slot-scope="s">-->
<!--          <span v-html="s.row.dayPrice"></span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column
        prop="faultName"
        label="故障说明"
        width="120">
      </el-table-column>
      <el-table-column
          prop="cityName"
          label="省/市/区"
          width="200">
        <template v-slot="r">
          {{r.row.provinceName}}  - {{r.row.cityName}} - {{r.row.districtName}}
        </template>
      </el-table-column>
      <el-table-column
          prop="pic"
          label="车辆图片"
          width="120">
        <template v-slot="s">
          <el-image
              style="width: 100px;height: 100px"
              :src="s.row.pic"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="openUpdate(scope.row)" type="text" size="small">修改</el-button>
          <el-button @click="del(scope.row.vehicleId)" type="text" size="small">删除</el-button>
          <el-button @click="showAll(scope.row.vehicleId)" type="text" size="small">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @current-change="goPage"
        :page-size="5"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>

    <div>
      <el-button @click="zxt" type="primary">展示折线图</el-button>
      <el-button @click="zzt" type="primary">展示柱状图</el-button>
      <el-button @click="bt" type="primary">展示饼图</el-button>
    </div>

    <div id="main1" style="width: 594px;height: 500px;"></div>
    <div id="main2" style="width: 500px;height: 500px;"></div>
    <div id="main3" style="width: 580px;height: 600px;"></div>

    <el-dialog title="车辆添加记录" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="车牌号" :label-width="formLabelWidth">
          <el-input v-model="form.plateNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="车型" :label-width="formLabelWidth">
          <el-input v-model="form.vehicleType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="车辆品牌" :label-width="formLabelWidth">
          <el-input v-model="form.vehicleBrand" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="颜色" :label-width="formLabelWidth">
          <el-input v-model="form.color" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="车联系统版本" :label-width="formLabelWidth">
          <el-input v-model="form.vehicleEdition" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="制造年份" :label-width="formLabelWidth">
          <el-date-picker
              v-model="form.makeYear"
              type="date"
              placeholder="选择年">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="日租金" :label-width="formLabelWidth">
          <el-input v-model="form.dayPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="省/市/区" :label-width="formLabelWidth">
          <el-cascader
              v-model="form.cascaderValue"
              :options="options"
              :props="props"
              @change="getchange"
              clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="故障说明" :label-width="formLabelWidth">
          <el-select v-model="form.faultId" placeholder="请选择">
            <el-option
                v-for="item in faultList"
                :key="item.faultId"
                :label="item.faultType"
                :value="item.faultId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="车辆图片" :label-width="formLabelWidth">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:10020/vehicle/addPic"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="车辆记录修改" :visible.sync="dialogFormVisible1">
      <el-form :model="form1">
        <el-form-item label="车牌号" :label-width="formLabelWidth1">
          <el-input v-model="form1.plateNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="车型" :label-width="formLabelWidth1">
          <el-input v-model="form1.vehicleType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="车辆品牌" :label-width="formLabelWidth1">
          <el-input v-model="form1.vehicleBrand" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="颜色" :label-width="formLabelWidth1">
          <el-input v-model="form1.color" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="车联系统版本" :label-width="formLabelWidth1">
          <el-input v-model="form1.vehicleEdition" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="制造年份" :label-width="formLabelWidth1">
          <el-date-picker
              v-model="form1.makeYear"
              type="date"
              placeholder="选择年">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="日租金" :label-width="formLabelWidth">
          <el-input v-model="form1.dayPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="故障说明" :label-width="formLabelWidth1">
          <el-select v-model="form1.faultId" placeholder="请选择">
            <el-option
                v-for="item in faultList"
                :key="item.faultId"
                :label="item.faultType"
                :value="item.faultId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="省/市/区" :label-width="formLabelWidth1">
          <el-cascader
              v-model="areaIds"
              :options="options"
              :props="props"
              @change="getchange1"
              clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="车辆图片" :label-width="formLabelWidth1">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:10020/vehicle/addPic"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="详细信息" :visible.sync="dialogTableVisible">
      <el-form :model="form2">
        <el-form-item label="车辆唯一编号" :label-width="formLabelWidth">
          <el-input v-model="form2.vehicleId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动区域" :label-width="formLabelWidth">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>

import * as echarts from 'echarts';

export default {
  name: 'VehiclesView',
  data() {
    return {
      form2:{},
      tableData: [],
      formInline: {},
      total: 0,
      pageNum: 1,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      form: {
        cascaderValue:[],
      },
      props: {
        value: 'id',
        label: 'name',
        children: 'children'
      },
      options: [],
      dialogFormVisible1: false,
      formLabelWidth1: '120px',
      form1: {},
      faultList:[],
      couponList:[],
      customerList:[],
      zxtdata:[],
      zztdata:[],
      bingdata:[],
      imageUrl:'',
      dialogTableVisible: false,
      gridData:{},
      areaIds:[0,0,0],

    }
  }, methods: {
    gettype() {
      this.axios.post("/vehicle/gettype").then(res => {
        this.options = res.data.data;
      });
    },
    getchange(id){
      console.log(id);
      if (id.length > 0) {
        this.form.provinceId = id[0];
        this.form.cityId = id[1];
        this.form.districtId = id[2];
      } else {
        this.form.provinceId = '';
      }
    },
    getchange1(id)
    {
      console.log(id);
      if (id.length > 0) {
        this.form1.provinceId = id[0];
        this.form1.cityId = id[1];
        this.form1.districtId = id[2];
      }
    },
    list(){
      this.axios.post("/vehicle/list?pageNum="+this.pageNum,this.formInline).then(res=>{
        this.tableData=res.data.data.list;
        this.total=res.data.data.total;
      })
    },
    goPage(pageNum){
      this.pageNum=pageNum;
      this.list();
    },
    openAdd(){
      this.form={};
      this.imageUrl="";
      this.areaIds=[];
      this.gettype();
      this.form.provinceId = "";
      this.dialogFormVisible=true;
    },
    add(){
      this.axios.post("/vehicle/addOrUpdate",this.form).then(res=>{
        if (res.data.code==200){
          this.$message.success(res.data.msg);
          this.dialogFormVisible=false;
          this.list();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    openUpdate(row){
      this.areaIds=[];
      this.form1=row;
      this.imageUrl=row.pic;
      if(row.provinceId){
        this.areaIds.push(row.provinceId,row.cityId,row.districtId);
        console.log(row.provinceId)
        console.log(row.cityId)
        console.log(row.districtId)
      }
      console.log(this.areaIds);
      this.axios.post("/vehicle/search?vehicleId="+row.vehicleId).then(res=>{
        this.form1=res.data.data;
        this.form1.faultName=res.data.data.faultName;
      })
      this.dialogFormVisible1=true;
    },
    update(){
      this.axios.post("/vehicle/addOrUpdate",this.form1).then(res=>{
        if (res.data.code==200){
          this.$message.success(res.data.msg);
          this.dialogFormVisible1=false;
          this.list();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    del(vehicleId){
      this.axios.post("/vehicle/del?vehicleId="+vehicleId).then(res=>{
        if (res.data.code==200){
          this.$message.success(res.data.msg);
          this.list();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    showAll(id){
      this.dialogTableVisible=true;
      this.axios.post("/vehicle/list1?vehicleId="+id).then(res=>{
        this.form2=res.data.data;
        console.log(res.data.data)
      })

    },
    getExcel(){
      window.location.href="http://localhost:10020/vehicle/excel";
    },
    zxt(){
      this.axios.post("/vehicle/getZxt").then(res=>{
        this.zxtdata=res.data.data;
        let xdata=[];
        let ydata=[];

        console.log(res.data)
        this.zxtdata.forEach(element=>{
          xdata.push(element.xzhou);
          ydata.push(element.yzhou);
        })

        var chartDom = document.getElementById('main1');
        var myChart = echarts.init(chartDom);
        var option;

        option = {
          xAxis: {
            type: 'category',
            data: xdata
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: ydata,
              type: 'line',
              smooth: true
            }
          ]
        };

        option && myChart.setOption(option);
      })

    },
    zzt(){
      this.axios.post("/vehicle/getZzt").then(res=>{
        this.zztdata=res.data.data;
        let xdata=[];
        let ydata=[];

        console.log(res.data)
        this.zztdata.forEach(element=>{
          xdata.push(element.xzhou);
          ydata.push(element.yzhou);
        })

        var chartDom = document.getElementById('main2');
        var myChart = echarts.init(chartDom);
        var option;

        option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: xdata,
              axisTick: {
                alignWithLabel: true
              }
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: 'Direct',
              type: 'bar',
              barWidth: '60%',
              data: ydata
            }
          ]
        };

        option && myChart.setOption(option);

      })
    },
    bt(){
      this.axios.post("/vehicle/getBing").then(res=>{
        this.bingdata=res.data.data;

        const xzhouDate=[];
        this.bingdata.forEach(element => {
          xzhouDate.push({value:element.yzhou,name:element.xzhou})
          console.log(xzhouDate)

          var chartDom = document.getElementById('main3');
          var myChart = echarts.init(chartDom);
          var option;

          option = {
            legend: {
              top: 'bottom'
            },
            toolbox: {
              show: true,
              feature: {
                mark: { show: true },
                dataView: { show: true, readOnly: false },
                restore: { show: true },
                saveAsImage: { show: true }
              }
            },
            series: [
              {
                name: 'Nightingale Chart',
                type: 'pie',
                radius: [50, 250],
                center: ['50%', '50%'],
                roseType: 'area',
                itemStyle: {
                  borderRadius: 8
                },
                data: xzhouDate
              }
            ]
          };

          option && myChart.setOption(option);

        });
      })
    },
    getFault(){
      this.axios.post("/vehicle/getfault").then(res=>{
        this.faultList=res.data.data;
      })
    },
    // getConpon(){
    //   this.axios.post("/vehicle/getconpon").then(res=>{
    //     this.couponList=res.data.data;
    //   })
    // },
    // getCustomer(){
    //   this.axios.post("/vehicle/getcustomer").then(res=>{
    //     this.customerList=res.data.data;
    //   })
    // },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.pic=res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 5MB!');
      }
      return isJPG && isLt2M;
    }
  }, created() {
    this.list();
    this.gettype();
    this.getFault();
    // this.getConpon();
    // this.getCustomer();
  }
}
</script>

<style>
#main1 {
  float: left;
}
#main2 {
  float: left;
}
#main3 {
  float: right;
}

 .avatar-uploader .el-upload {
   border: 1px dashed #d9d9d9;
   border-radius: 6px;
   cursor: pointer;
   position: relative;
   overflow: hidden;
 }
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
